package com.example.hxds.bff.driver.controller.form;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.example.hxds.common.exception.HxdsException;
import com.example.hxds.common.util.CosUtil;
import com.example.hxds.common.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.io.IOException;
import java.util.HashMap;

import static io.protostuff.MapSchema.MessageFactories.Map;

@RestController
@RequestMapping("/cos")
@Slf4j
@Tag(name = "CosController", description = "对象存储Web接口")
public class CosController {

  @Autowired
  private CosUtil cosUtil;

  @PostMapping("/uploadCosPrivateFile")
  @SaCheckLogin
  @Operation(summary = "上传文件")
  public R uploadCosPrivateFile(@Param("file") MultipartFile file, @Param("module") String module) {
    if (file.isEmpty()) {
      throw new HxdsException("上传文件不能为空");
    }
    try {
      String path = "";
      if ("driverAuth".equals(module)) {
        path = "/driver/auth";
      } else {
        throw new HxdsException("module错误");
      }
      HashMap map = cosUtil.uploadPrivateFile(file, path);
      return R.ok(map);
    } catch (IOException e) {
      log.error("文件上传到腾讯云错误", e);
      throw new HxdsException("件上传到腾讯云错误");
    }
  }

  @PostMapping("/deleteCosPrivateFile")
  public R deleteCosPrivateFile(@Valid @RequestBody DeleteCosFileForm form) {
    cosUtil.deletePrivateFile(form.getPaths());
    return R.ok();
  }
}
